<template>
	<view>
		<view class="cardBg">
			<u-button type="primary" text="1初始化蓝牙" @click="initBlue()"></u-button>
			<u-button type="primary" text="2搜索附近蓝牙设备" @click="findBlue()"></u-button>
			<!-- <u-button type="primary" text="2-1test" @click="test()"></u-button> -->
			<!-- 	<u-button type="primary" text="3获取蓝牙服务"></u-button>
			<u-button type="primary" text="4获取特征值"></u-button>
			<u-button type="primary" text="5开启消息监听"></u-button>
			<u-button type="primary" text="6发送数据"></u-button>
			<u-button type="primary" text="7读取数据"></u-button> -->
		</view>
		<view class="cardBg">
			<view class="cardbox">
				<uni-section title="蓝牙服务" type="line" />
			</view>
			<view class="row" v-for="(item,index) in bluetoothServices">
				<view class="left">
					<text>{{(index+1)}}</text>
					<view class="content">
						<view class="text"><text class="lable">Serviceuuid：</text>
							{{item.uuid}}
						</view>
						<view class="name"><text class="lable">isPrimary：</text>
							{{item.isPrimary}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="cardBg">
			<view class="cardbox">
				<uni-section title="蓝牙特征值" type="line" />
			</view>
			<view class="row" v-for="(item,index) in bluetoothCharacteristics">
				<view class="left">
					<text>{{(index+1)}}</text>
					<view class="content">
						<view class="text"><text class="lable">uuid：</text>
							{{item.uuid}}
						</view>
						<view class="name">
							<text class="lable">indicate：</text>
							{{item.properties.indicate}}
						</view>
						<view class="name">
							<text class="lable">notify：</text>
							{{item.properties.notify}}
						</view>
						<view class="name">
							<text class="lable">read：</text>
							{{item.properties.read}}
						</view>
						<view class="name">
							<text class="lable">write：</text>
							{{item.properties.write}}
						</view>
						<view class="name">
							<text class="lable">writeDefault：</text>
							{{item.properties.writeDefault}}
						</view>
						<view class="name">
							<text class="lable">writeNoResponse：</text>
							{{item.properties.writeNoResponse}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="cardBg">
			<view class="cardbox">
				<uni-section title="蓝牙列表" type="line" />
			</view>
			<view class="row" v-for="(item,index) in getBluetoothDevices">
				<view class="left">
					<text>{{(index+1)}}</text>
					<view class="content">
						<view class="name"><text class="lable">name：</text> {{item.name}}</view>
						<view class="text" @click="connetBlue(item.deviceId)"><text class="lable">uuid：</text>
							{{item.deviceId}}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timeValue: Number(new Date()),
				getBluetoothDevices: [], // 存储搜索到的蓝牙设备
				bluetoothServices: [], // 存储蓝牙服务信息
				bluetoothCharacteristics: [], //存储蓝牙特征值信息
				deviceId: '', //蓝牙设备id
				readyservices: '', //因设备而议：该特征值只支持读
				services: '', //因设备而议：该特征值支持write和notfy服务
				notifyUuid: '', //特征值
			}
		},
		methods: {
			// 1、初始化蓝牙设备
			initBlue() {
				var that = this;
				uni.openBluetoothAdapter({ //调用微信小程序api 打开蓝牙适配器接口
					success: function(res) {
						console.log('初始化蓝牙设备~~', res)
						uni.showToast({
							title: '初始化成功',
							icon: 'success',
							duration: 800
						})
					},
					fail: function(res) { //如果手机上的蓝牙没有打开，可以提醒用户
						uni.showToast({
							title: '请打开蓝牙',
							type: 'error',
							icon: 'none'
						});
					}
				})
			},
			// 2、搜索周边设备
			findBlue() {
				var that = this
				uni.startBluetoothDevicesDiscovery({
					allowDuplicatesKey: false,
					interval: 0,
					success: function(res) {
						console.log('搜索周边设备~~', res)
						uni.showLoading({
							title: '正在搜索设备',
						})
						that.getBlue() //3.0
					}
				})
			},
			// 3.获取搜索到的设备信息
			getBlue() {
				var that = this
				//uni.getBluetoothDevices获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备
				uni.getBluetoothDevices({
					success: function(res) {
						uni.hideLoading();
						console.log('获取搜索到的设备信息~~', res)
						that.getBluetoothDevices = res.devices
						//将BluetoothList遍历给用户，当用户点击连接某个蓝牙时调用4.0
					},
					fail: function() {
						console.log("搜索蓝牙设备失败")
					}
				})
			},
			// test() {
			// 	uni.onBluetoothDeviceFound(res => {
			// 		console.log('监听蓝牙设备列表', res)
			// 		// res.devices.forEach(device => {
			// 		// 	if (device.name.indexOf(deviceName) == -1) {
			// 		// 		return;
			// 		// 	}
			// 		// 	createBLEConnection(device, callBack);
			// 		// });
			// 	});
			// },



			// 4.当用户点击某个设备时将deviceId进行蓝牙连接
			connetBlue(deviceId) {
				console.log('deviceId~~~', deviceId)
				var that = this;
				uni.createBLEConnection({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: deviceId, //设备id
					success: function(res) {
						uni.showToast({
							title: '连接成功',
							icon: 'fails',
							duration: 800
						})
						console.log("连接蓝牙成功!-->11111")
						uni.stopBluetoothDevicesDiscovery({
							success: function(res) {
								console.log('连接蓝牙成功之后关闭蓝牙搜索');
								uni.showToast({
									title: '连接蓝牙成功~~~关闭蓝牙搜索',
									icon: 'fails',
									duration: 800
								})
							}
						})
						that.deviceId = deviceId;
						that.getServiceId() //5.0
					}
				})
			},
			// 5.连接上需要的蓝牙设备之后，获取这个蓝牙设备的服务uuid
			getServiceId() {
				var that = this
				console.log('deviceId~~~~5.0', that.deviceId);
				uni.getBLEDeviceServices({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: that.deviceId,
					success: function(res) {
						console.log('蓝牙设备的服务uuid~~~5.0', res)
						//需要什么服务就用对应的services
						that.bluetoothServices = res.services
						that.readyservices = res.services[0].uuid //因设备而议：该特征值只支持读
						that.services = res.services[1].uuid //因设备而议：该特征值支持write和notfy服务
						that.getCharacteId() //6.0
					}
				})
			},
			// 6.如果一个蓝牙设备需要进行数据的写入以及数据传输，就必须具有某些特征值，所以通过上面步骤获取的id可以查看当前蓝牙设备的特征值
			getCharacteId() {
				var that = this
				uni.getBLEDeviceCharacteristics({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: that.deviceId,
					// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
					serviceId: that.services,
					success: function(res) {
						that.bluetoothCharacteristics = res.characteristics
						console.log('蓝牙特征值~~~6.0', res)
						// for (var i = 0; i < res.characteristics.length; i++) { //2个值
						// 	var model = res.characteristics[i]
						// 	if (model.properties.write) {
						// 		//model.uuid:用来写入的uuid值
						// 		//this.sendMy()给设备写入
						// 		// that.sendMy(model.uuid)
						// 	}
						// 	if (model.properties.notify) {
						// 		//model.uuid:用来notify的uuid值
						// 		that.notifyUuid = model.uuid
						// 	}
						// }
					}
				})
			},




		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-button {
		margin: 5px 0 !important;
	}

	.cardbox {
		display: flex;
		align-items: center;
		justify-content: space-between;

		.time {
			font-size: 12px;
			background: #eee;
			border-radius: 4px;
			padding: 0px 10px;
			display: flex;
			align-items: center;
			color: #676767;
		}
	}

	.row {
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1px solid #eee;
		position: relative;
		background: #fff;

		&:last-child {
			border-bottom: 0
		}

		.left {
			display: flex;
			align-items: center;

			.content {
				padding-left: 20rpx;

				.text {
					color: #666;
					font-size: 14px;

					.lable {
						color: #0066cc;
						font-size: 15px;
					}
				}

				.name {
					color: #666;
					font-size: 14px;

					.lable {
						color: #0066cc;
						font-size: 15px;
					}
				}
			}

		}
	}
</style>